<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        button {
            margin: 16px;
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            color: white;
            padding: 0 18px;
            height: 38px;
            border: 1px solid transparent;
            background-color: #16baaa;
            border-radius: 2px;
            box-sizing: border-box;
            cursor: pointer;
        }

        button:hover {
            opacity: .8;
        }

        .icon {
            font-size: 16px;
            font-style: normal;
            display: inline-block;
            pointer-events: none;
        }

        .icon::before {
            content: "❤";
        }


        .active {
            animation: anim2 0.3s ease-in-out;
        }

        .icon {
            font-size: 16px;
            font-style: normal;
            display: inline-block;
            pointer-events: none;
        }

        .icon::before {
            content: "❤";
        }


        .scale-anim {
            animation: anim 0.3s linear;
        }

        @keyframes anim {
            from {
                transform: scale(0.1);
            }

            to {
                transform: scale(1);
            }
        }
    </style>
</head>

<body>
    <button id="btn1"><i class="icon"></i>按钮一</button>
    <button id="btn2"><i class="icon"></i>按钮二</button>
    <button id="btn3"><i class="icon"></i>按钮三</button>
    <button id="btn4"><i class="icon"></i>按钮四</button>
</body>
<script>
    const btn1 = document.querySelector('#btn1');
    const btn2 = document.querySelector('#btn2');
    const btn3 = document.querySelector('#btn3');
    const btn4 = document.querySelector('#btn4');
    /**
     * 在点击事件里直接执行classList.remove('scale-anim')和classList.add('scale-anim')是不会触发动画的
     * 因为在元素上移除并立即添加一个类时，浏览器可能没有足够的时间来更新样式和触发动画。为了确保动画被触发，可以
     * 使用requestAnimationFrame或者setTimeout来确保在添加类之前浏览器有足够的时间来更新样式并计算动画。
     */

    // 方式一：使用requestAnimationFrame或者setTimeout
    btn1.addEventListener('click', (e) => {
        e.target.classList.remove('scale-anim');
        requestAnimationFrame(() => {
            e.target.classList.add('scale-anim');
        });
    });
    btn2.addEventListener('click', (e) => {
        e.target.classList.remove('scale-anim');
        setTimeout(() => {
            e.target.classList.add('scale-anim');
        }, 0);
    });

    // 方式二：监听animationend事件，动画结束后移除动画类
    btn3.addEventListener('click', (e) => {
        e.target.classList.add('scale-anim');
    });
    btn3.addEventListener('animationend', (e) => {
        e.target.classList.remove('scale-anim');
    });

    // 方式三：通过dom元素的animate方法
    let anim = btn4.animate([{ offset: 0, scale: 0.1 }, { offset: 1, scale: 1 }],{ id: 'anim', duration: 300 });
    anim.cancel();
    btn4.addEventListener('click', (e) => {
        if (anim.playState !== 'running') {
            anim.play();
        }
    });
</script>

</html>